﻿using System;
using System.Runtime.Serialization;

namespace FitAutomation
{
	[DataContract]
	public class Identifier
	{
		string id;

		[DataMember]
		public string Id
		{
			get { return id; }
			set
			{
				if (id != null)
				{
					throw new InvalidOperationException("value already defined");
				}
				id = value;
			}
		}

		public Identifier()
		{
			
		}

		public Identifier(Guid id)
		{
			Id = id.ToString();
		}

		public Identifier(string id)
		{
			Id = id;
		}

		public static implicit operator Identifier(Guid id)
		{
			return new Identifier(id);
		}

		public static implicit operator Identifier(string id)
		{
			return new Identifier(id);
		}

		public override bool Equals(object obj)
		{
			if (ReferenceEquals(null, obj)) return false;
			if (ReferenceEquals(this, obj)) return true;
			if (obj.GetType() != GetType()) return false;
			return Equals((Identifier) obj);
		}

		public bool Equals(Identifier other)
		{
			if (ReferenceEquals(null, other)) return false;
			if (ReferenceEquals(this, other)) return true;
			return Equals(other.id, id);
		}

		public override int GetHashCode()
		{
			return (id != null ? id.GetHashCode() : 0);
		}

		public override string ToString()
		{
			return Id ?? "NullIdentifier";
		}
	}
}